#region (c) 2014 Nguyễn, M. Hải  - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, Nguyễn, M. Hải ( www.Minh-Hai.com ). All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

#region Record of Change
//             							CHANGE HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | DEVELOPER  | DESCRIPTION                                              |
// -------------------------------------------------------------------------------------
// | 03-Oct-12 | HaiNM      | First creation.                                          |
// -------------------------------------------------------------------------------------
//
#endregion

#region Record of Audit
//             							REVIEW HISTORY
// -------------------------------------------------------------------------------------
// |   DATE    | REVIEWER   | NOTES                                                    |
// -------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------
//
#endregion

#region Using directives
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Navigation;

using Thn;
using Thn.Presentation.Controls;
#endregion

namespace Thn.Presentation.Controls
{
    /// <summary>
    /// 
    /// </summary>
#if DEBUG 
    public
#else
    public
#endif 
    partial class NumberLabel : UserControl
    {
        #region Fields
        #endregion

        #region Properties

        #region Value
        /// <summary>
        /// Gets or sets value.
        /// </summary>
        [DefaultValue(0)]
        public decimal Value
        {
            get { return (decimal)GetValue(ValueProperty); }
            set
            {
                SetValue(ValueProperty, value);
                //ApplyValueToTextWithFormat();
            }
        }

        /// <summary>
        /// Backing dependency property for Value.
        /// </summary>
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register(
            "Value"
            , typeof(decimal)
            , typeof(NumberLabel)
            , new PropertyMetadata(0m, ValuePropertyChanged)
            );

        /// <summary>
        /// Invoked when value of property Value is changed.
        /// </summary>
        protected virtual void OnValueChanged(DependencyPropertyChangedEventArgs e)
        {

        }

        /// <summary>
        /// Static handler for listening to changes of ValueProperty.
        /// </summary>
        private static void ValuePropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            NumberLabel obj = (NumberLabel)target;

            //update target object
            obj.OnValueChanged(e);

            //raise event
            //obj.RaiseEventPropertyChanged("Value");

            obj.ApplyValueToTextWithFormat();
        }
        #endregion

        #region Format
        private string mFormat = "#,#0.#####";
        /// <summary>
        /// Gets or sets description.
        /// </summary>
        public string Format
        {
            get { return mFormat; }
            set
            {
                mFormat = value;
                ApplyValueToTextWithFormat();
            }
        }
        #endregion

        #region Text
        /// <summary>
        /// Gets or sets display text.
        /// </summary>
        [DefaultValue("")]
        public string Text
        {
            get { return (string)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        }

        /// <summary>
        /// Backing dependency property for Text.
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register(
            "Text"
            , typeof(string)
            , typeof(NumberLabel)
            , new PropertyMetadata("", TextPropertyChanged)
            );

        /// <summary>
        /// Invoked when value of property Text is changed.
        /// </summary>
        protected virtual void OnTextChanged(DependencyPropertyChangedEventArgs e)
        {
        }

        /// <summary>
        /// Static handler for listening to changes of TextProperty.
        /// </summary>
        private static void TextPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
        {
            NumberLabel obj = (NumberLabel)target;

            //update target object
            obj.OnTextChanged(e);
        }
        #endregion

        #region FontWeight
        /// <summary>
        /// Gets or sets description.
        /// </summary>
        public FontWeight FontWeight
        {
            get { return txtDisplay.FontWeight; }
            set { txtDisplay.FontWeight = value; }
        }
        #endregion
        

        #endregion

        #region UI Events processing
        #endregion

        #region Apply Value To Text With Format
        private void ApplyValueToTextWithFormat()
        {
            if (!string.IsNullOrEmpty(mFormat))
            {
                string format = "{0:" + mFormat + "}";
                string txt = string.Format(format, this.Value);
                this.Text = txt;
            }
        }
        #endregion
        
        #region On Loaded
        /// <summary>
        /// Initialize this form when form is loaded.
        /// </summary>
        void OnLoaded(object sender, RoutedEventArgs e)
        {
			//if (!this.IsDesignMode())
            {

		

            }
            //#region Defensive tracing
            //else
            //{
            //    //do nothing in design-mode
            //}
            //#endregion
        }
        #endregion
        
        #region Constructors
        /// <summary>
        /// Default constructor.
        /// </summary>
        public NumberLabel() 
            : base()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(OnLoaded);
            this.SetValue(VerticalAlignmentProperty, VerticalAlignment.Center);
            this.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Right);
            this.Margin = new Thickness(5);
        }
        #endregion
    }
}
